
#import "GFAlertView.h"
#import "UILabel+NSMutableAttributedString.h"

#define kNormalSize CGSizeMake(315, 175)
#define kBigSize CGSizeMake(315, 300)

@interface GFAlertView ()

@property (nonatomic, strong) UIView *coverView;

@property (nonatomic, strong) UIImageView *alertIcon;
@property (nonatomic, strong) UIImageView *closeIcon;

@end

@implementation GFAlertView


#pragma mark - Private Methods

+ (instancetype)initAlertView {
    
    GFAlertView *alertView = [[GFAlertView alloc] init];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.layer.cornerRadius = kScaleNum(10.0);
    alertView.typeOfSize = TypeSizeNormal;
    
    return alertView;
}

+ (instancetype)alertViewWithMsg:(NSString *)msg detailMsg:(NSString *)detailMsg knowBtnBlock:(void(^)(UIButton *knowBtn))knowBtnBlock {
    GFAlertView *alertView = [GFAlertView initAlertView];
    
    if (!knowBtnBlock) {
        return alertView;
    }
    
    [alertView addSubview:alertView.titleLab];
    alertView.titleLab.text = msg;
    [alertView.titleLab setLineSpace:8];
    [alertView.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kScaleNum(20)));
        make.right.equalTo(@(kScaleNum(-20)));
        make.top.equalTo(@(kScaleNum(20)));
    }];
    
    [alertView addSubview:alertView.detailLab];
    alertView.detailLab.text = detailMsg;
    [alertView.detailLab setLineSpace:6];
    [alertView.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kScaleNum(20)));
        make.right.equalTo(@(kScaleNum(-20)));
        make.top.equalTo(alertView.titleLab.mas_bottom).with.offset(kScaleNum(8));
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitleColor:kLightgGrayColor forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = kFont(18);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [alertView addSubview:cancelBtn];
    [cancelBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(@0);
        make.width.mas_equalTo(alertView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [cancelBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kFont(18);
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [alertView addSubview:rightBtn];
    [rightBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(@0);
        make.width.mas_equalTo(alertView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [rightBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *horiLine = [[UIView alloc] init];
    horiLine.backgroundColor = kLineColor;
    [alertView addSubview:horiLine];
    [horiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(cancelBtn);
        make.height.mas_equalTo(1.0);
    }];
    
    UIView *vertiLine = [[UIView alloc] init];
    vertiLine.backgroundColor = kLineColor;
    [alertView addSubview:vertiLine];
    [vertiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(rightBtn);
        make.width.mas_equalTo(1.0);
    }];
    
    if (knowBtnBlock) {
        knowBtnBlock(rightBtn);
    }
    
    return alertView;
}

+ (instancetype)alertViewWithMsg:(NSString *)msg detailMsg:(NSString *)detailMsg leftBtnBlock:(void(^)(UIButton *leftBtn))leftBtnBlock
                   rightBtnBlock:(void(^)(UIButton *rightBtn))rightBtnBlock {
    GFAlertView *alertView = [GFAlertView initAlertView];
    
    if (!leftBtnBlock) {
        return alertView;
    }
    
    if (!rightBtnBlock) {
        return alertView;
    }
    
    [alertView addSubview:alertView.titleLab];
    alertView.titleLab.text = msg;
    [alertView.titleLab setLineSpace:8];
    [alertView.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kScaleNum(20)));
        make.right.equalTo(@(kScaleNum(-20)));
        make.top.equalTo(@(kScaleNum(20)));
    }];
    
    [alertView addSubview:alertView.detailLab];
    alertView.detailLab.text = detailMsg;
    [alertView.detailLab setLineSpace:6];
    [alertView.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kScaleNum(20)));
        make.right.equalTo(@(kScaleNum(-20)));
        make.top.equalTo(alertView.titleLab.mas_bottom).with.offset(kScaleNum(8));
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitleColor:kLightgGrayColor forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = kFont(18);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [alertView addSubview:cancelBtn];
    [cancelBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(@0);
        make.width.mas_equalTo(alertView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [cancelBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitleColor:kRedColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kFont(18);
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [alertView addSubview:rightBtn];
    [rightBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(@0);
        make.width.mas_equalTo(alertView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [rightBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *horiLine = [[UIView alloc] init];
    horiLine.backgroundColor = kLineColor;
    [alertView addSubview:horiLine];
    [horiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(cancelBtn);
        make.height.mas_equalTo(1.0);
    }];
    
    UIView *vertiLine = [[UIView alloc] init];
    vertiLine.backgroundColor = kLineColor;
    [alertView addSubview:vertiLine];
    [vertiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(rightBtn);
        make.width.mas_equalTo(1.0);
    }];
    
    if (leftBtnBlock) {
        leftBtnBlock(cancelBtn);
    }
    
    if (rightBtnBlock) {
        rightBtnBlock(rightBtn);
    }
    
    return alertView;
}

+ (instancetype)alertViewWithIconType:(TypeOfIcon)iconType msg:(NSString *)msg {
    
    GFAlertView *alertView = [GFAlertView initAlertView];
    
    UIImageView *alertIcon = [[UIImageView alloc] init];
    [alertView addSubview:alertIcon];
    [alertIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(kScaleNum(55)));
        make.centerX.equalTo(@0);
        make.size.mas_equalTo(SASizeMake(40, 40));
    }];
    if (iconType == TypeOfIconWarn) {
        alertIcon.image = [UIImage imageNamed:@"win_warning"];
    } else if (iconType == TypeOfIconTick) {
        alertIcon.image = [UIImage imageNamed:@"win_success"];
    } else if (iconType == TypeOfIconBlueDoor) {
        alertIcon.image = [UIImage imageNamed:@"icon_bluedoor"];
        [alertIcon mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(SASizeMake(58, 35));
        }];
    }
    
    alertView.alertIcon = alertIcon;
    
    [alertView addSubview:alertView.titleLab];
    alertView.titleLab.text = msg;
    [alertView.titleLab setLineSpace:8];
    [alertView.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kScaleNum(20)));
        make.right.equalTo(@(kScaleNum(-20)));
        make.top.equalTo(@(kScaleNum(115)));
    }];
//    [alertView.titleLab sizeToFit];
    
    return alertView;
}

+ (instancetype)alertViewWithIconType:(TypeOfIcon)iconType msg:(NSString *)msg rightBtnBlock:(void (^)(UIButton *))rightBtnBlock {
    
    GFAlertView *alertView = [GFAlertView alertViewWithIconType:iconType msg:msg];
    
    if (!rightBtnBlock) {
        return alertView;
    }
    alertView.alertIcon.hidden = YES;
    
    [alertView.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(kScaleNum(35)));
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitleColor:kLightgGrayColor forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = kFont(18);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [alertView addSubview:cancelBtn];
    [cancelBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(@0);
        make.width.mas_equalTo(alertView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [cancelBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitleColor:kRedColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kFont(18);
    [alertView addSubview:rightBtn];
    [rightBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(@0);
        make.width.mas_equalTo(alertView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [rightBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *horiLine = [[UIView alloc] init];
    horiLine.backgroundColor = kLineColor;
    [alertView addSubview:horiLine];
    [horiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(cancelBtn);
        make.height.mas_equalTo(1.0);
    }];
    
    UIView *vertiLine = [[UIView alloc] init];
    vertiLine.backgroundColor = kLineColor;
    [alertView addSubview:vertiLine];
    [vertiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(rightBtn);
        make.width.mas_equalTo(1.0);
    }];
    
    if (rightBtnBlock) {
        rightBtnBlock(rightBtn);
    }
    
    return alertView;
}

+ (instancetype)alertViewWithIconType:(TypeOfIcon)iconType msg:(NSString *)msg knowBtnBlock:(void (^)(UIButton *))knowBtnBlock {
    
    GFAlertView *alertView = [GFAlertView alertViewWithIconType:iconType msg:msg];
    
    [alertView.alertIcon mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(kScaleNum(35)));
    }];
    
    [alertView.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(kScaleNum(85)));
    }];
    alertView.closeIcon.hidden = YES;
    
    UIButton *knowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [knowBtn setTitleColor:kRedColor forState:UIControlStateNormal];
    knowBtn.titleLabel.font = kFont(18);
    [knowBtn setTitle:@"知道了" forState:UIControlStateNormal];
    [alertView addSubview:knowBtn];
    [knowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [knowBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    if (knowBtnBlock) {
        knowBtnBlock(knowBtn);
    }
    
    UIView *horiLine = [[UIView alloc] init];
    horiLine.backgroundColor = kLineColor;
    [alertView addSubview:horiLine];
    [horiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(knowBtn);
        make.height.mas_equalTo(1.0);
    }];
    
    return alertView;
}

+ (instancetype)alertViewWithIconType:(TypeOfIcon)iconType msg:(NSString *)msg detailMsg:(NSString *)detailMsg knowBtnBlock:(void (^)(UIButton *))knowBtnBlock {
    
    GFAlertView *alertView = [GFAlertView alertViewWithIconType:iconType msg:msg knowBtnBlock:knowBtnBlock];
    
    [alertView addSubview:alertView.detailLab];
    alertView.detailLab.text = detailMsg;
    [alertView.detailLab setLineSpace:6];
    [alertView.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kScaleNum(20)));
        make.right.equalTo(@(kScaleNum(-20)));
        make.top.equalTo(alertView.titleLab.mas_bottom).with.offset(kScaleNum(8));
    }];
    
    return alertView;
}

+ (instancetype)alertViewWithIconType:(TypeOfIcon)iconType msg:(NSString *)msg leftBtnBlock:(void (^)(UIButton *))leftBtnBlock rightBtnBlock:(void (^)(UIButton *))rightBtnBlock {
    
    GFAlertView *alertView = [GFAlertView alertViewWithIconType:iconType msg:msg];
    
    [alertView.alertIcon mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(kScaleNum(35)));
    }];
    NSArray *line = [msg componentsSeparatedByString:@"\n"];
    if (line.count >= 3) {
        [alertView.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@(kScaleNum(85)));
        }];
    }else{
        [alertView.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@(kScaleNum(100)));
        }];;
    }
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitleColor:kRedColor forState:UIControlStateNormal];
    leftBtn.titleLabel.font = kFont(18);
    [alertView addSubview:leftBtn];
    [leftBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(@0);
        make.width.mas_equalTo(alertView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [leftBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitleColor:kRedColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kFont(18);
    [alertView addSubview:rightBtn];
    [rightBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(@0);
        make.width.mas_equalTo(alertView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(kScaleNum(50));
    }];
    [rightBtn addTarget:alertView action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *horiLine = [[UIView alloc] init];
    horiLine.backgroundColor = kLineColor;
    [alertView addSubview:horiLine];
    [horiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(leftBtn);
        make.height.mas_equalTo(1.0);
    }];
    
    UIView *vertiLine = [[UIView alloc] init];
    vertiLine.backgroundColor = kRedColor;
    [alertView addSubview:vertiLine];
    [vertiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(rightBtn);
        make.width.mas_equalTo(1.0);
    }];
    
    if (leftBtnBlock) {
        leftBtnBlock(leftBtn);
    }
    if (rightBtnBlock) {
        rightBtnBlock(rightBtn);
    }
    
    return alertView;
}

+ (instancetype)alertViewWithIconType:(TypeOfIcon)iconType
                                  msg:(NSString *)msg
                            detailMsg:(NSString *)detailMsg
                         letfBtnBlock:(void(^)(UIButton *leftBtn))leftBtnBlock
                        rightBtnBlock:(void(^)(UIButton *rightBtn))rightBtnBlock{
    
    GFAlertView *alertView = [GFAlertView alertViewWithIconType:iconType msg:msg leftBtnBlock:leftBtnBlock rightBtnBlock:rightBtnBlock];
    NSArray *line = [msg componentsSeparatedByString:@"\n"];
    if (line.count >= 2) {
        alertView.titleLab.y = kScaleNum(75);
    }else{
        alertView.titleLab.y = kScaleNum(90);
    }
    
    [alertView addSubview:alertView.detailLab];
    alertView.detailLab.text = detailMsg;
    [alertView.detailLab setLineSpace:6];
    [alertView.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kScaleNum(20)));
        make.right.equalTo(@(kScaleNum(-20)));
        make.top.equalTo(alertView.titleLab.mas_bottom).with.offset(kScaleNum(8));
    }];
    
    return alertView;
}

#pragma mark - 显示相关

- (void)showWithAnimationOnView:(UIView *)view {
    
    [self initUI];
    
    [view addSubview:self.coverView];
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    [view addSubview:self];
    CGSize size = CGSizeZero;
    if (_typeOfSize == TypeSizeNormal) {
        size = kNormalSize;
    } else if (_typeOfSize == TypeSizeBig) {
        size = kBigSize;
    }
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@0);
        make.centerX.equalTo(@0);
        make.size.mas_equalTo(SASizeMake(size.width, size.height));
    }];

    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.duration = 0.1;
    animation.fromValue = @(1.05);
    animation.toValue = @(1);
    animation.beginTime = 0;
    animation.removedOnCompletion = YES;
    
    [self.layer addAnimation:animation forKey:nil];
}

- (void)hiddenClose {
    self.closeIcon.hidden = YES;
}

- (void)showWithAnimation {
    [self showWithAnimationOnView:[UIApplication sharedApplication].keyWindow];
}

- (void)removeFromSuperview {
    [super removeFromSuperview];
    [self.coverView removeFromSuperview];
}

#pragma mark - 搭建UI

- (void)initUI {
    
    [self addSubview:self.closeIcon];
    [self.closeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(@0);
        make.size.mas_equalTo(SASizeMake(35, 35));
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeFromSuperview)];
    self.closeIcon.userInteractionEnabled = YES;
    [self.closeIcon addGestureRecognizer:tap];
}

#pragma mark - Setters

- (void)setTypeOfSize:(TypeOfSize)typeOfSize {
    
    _typeOfSize = typeOfSize;
}

#pragma mark - Getters

- (UIView *)coverView {
    if (!_coverView) {
        _coverView = [[UIView alloc] init];
        _coverView.backgroundColor = kColor(0, 0, 0, 0.4);
    }
    return _coverView;
}

- (UIImageView *)closeIcon {
    if (!_closeIcon) {
        _closeIcon = [[UIImageView alloc] init];
        _closeIcon.image = [UIImage imageNamed:@"close-g"];
        _closeIcon.contentMode = UIViewContentModeCenter;
    }
    return _closeIcon;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor blackColor];
        _titleLab.font = kFont(20);
        _titleLab.numberOfLines = 0;
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}

- (UILabel *)detailLab {
    if (!_detailLab) {
        _detailLab = [[UILabel alloc] init];
        _detailLab.textColor = kGrayColor;
        _detailLab.font = kFont(18);
        _detailLab.numberOfLines = 0;
        _detailLab.textAlignment = NSTextAlignmentCenter;
    }
    return _detailLab;
}

@end
